package com.tdcoding.chat.dbase;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tdcoding.chat.entities.ChatInfo;
import com.tdcoding.chat.helper.Logger;
import com.tdcoding.chat.manager.DBManager;

/**
 * 离线消息管理
 * @author Wellsa
 *
 */
public class DBForChat {
	private static final String TAG="DBForChat";
	private static final String MESSAGE_TABLE="ofMessage";
	
	/**
	 * 查询离线消息
	 * @param userId
	 * @param chatState
	 * @return
	 */
	public static List<ChatInfo> queryChatInfo(long userId,int chatState) {
		Logger.println(TAG, "queryChatInfo", userId);
		String orderBy="chatDate desc";
		String where = "chatToId="+userId+" or chatToRid="+userId +" and chatState="+chatState;
		List<ChatInfo> chatInfos=new ArrayList<ChatInfo>();
		try {
			ResultSet resultSet = DBManager.querySql(0, 
					"ofMessage", null, where, orderBy, null, null);
			while (resultSet.next()) {
				ChatInfo chatInfo =new ChatInfo();
				chatInfo.setPacketId(resultSet.getString("packetId"));
				chatInfo.setChatId(resultSet.getLong("chatId"));
				chatInfo.setChatName(resultSet.getString("chatName"));
				
				chatInfo.setChatToId(resultSet.getLong("chatToId"));
				chatInfo.setChatToName(resultSet.getString("chatToName"));
				
				chatInfo.setChatHead(resultSet.getString("chatHead"));
				chatInfo.setChatToHead(resultSet.getString("chatToHead"));
				
				chatInfo.setChatType(resultSet.getInt("chatType"));
				chatInfo.setBodyType(resultSet.getInt("bodyType"));
				chatInfo.setDuration(resultSet.getInt("duration"));
				chatInfo.setChatDate(resultSet.getString("chatDate"));
				chatInfo.setChatContent(resultSet.getString("content"));
				chatInfos.add(chatInfo);
			}
			return chatInfos;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 添加离线消息
	 * @param pullState
	 * @param chatInfo
	 * @return
	 */
	final static String COLUMNS="packetId,chatId,chatHead,chatName,chatToId,chatToRid,chatToHead,chatToName,"
			+ "chatType,bodyType,duration,chatState,chatDate,content";
	public static int addChatInfo(int chatState,ChatInfo chatInfo){
		String values="'"+chatInfo.getPacketId()
				+"',"+chatInfo.getChatId()
				+",'"+chatInfo.getChatHead()
				+"','"+chatInfo.getChatName()
				+"',"+chatInfo.getChatToId()
				+","+chatInfo.getChatToRid()
				+",'"+chatInfo.getChatToHead()
				+"','"+chatInfo.getChatToName()
				+"',"+chatInfo.getChatType()
				+","+chatInfo.getBodyType()
				+","+chatInfo.getDuration()
				+","+chatState
				+",'"+chatInfo.getChatDate()
				+"','"+chatInfo.getChatContent()+"'";
		return DBManager.insertSql(0,MESSAGE_TABLE, COLUMNS,values);
	}
	
	/**
	 * 清除离线消息
	 * @param userId
	 * @return
	 */
	public static int clearChatInfo(long userId){
		String where="chatToId="+userId+" or chatToRid="+userId;
		return DBManager.deleteSql(0,MESSAGE_TABLE, where);
	}
	
	/**
	 * 更新离线消息
	 * @param set
	 * @param where
	 * @return
	 */
	public static int updateChatInfo(String set,String where){
		return DBManager.updateSql(0,MESSAGE_TABLE, set, where);
	}
}
